package com.tree.thrive.business.excel;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: cheng.tang
 * @Date: 2023/8/30
 * @Description: top-tree
 */
@Service
@Slf4j
public class ExcelReaderServiceImpl implements ExcelReaderService {

    @Override
    public List<String> readAll(InputStream inputStream) {
        List<String> skuNoList = new ArrayList<>();
        org.apache.poi.util.IOUtils.setByteArrayMaxOverride(304101189);
        ExcelUtil.readBySax(inputStream, 0, new RowHandler() {
            @Override
            public void handle(int sheetIndex, long rowIndex, List<Object> list) {
                log.info(" sheetIndex {} rowIndex {} list {} ", sheetIndex, rowIndex, list);
                for (Object cellVal : list) {
                    if (cellVal != null) {
                        String skuVal = cellVal.toString();
                        if (StringUtils.isNotBlank(skuVal)) {
                            skuNoList.add(skuVal.trim());
                        }
                    }
                }
            }
        });
        return skuNoList;
    }

}
